﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace TryTM
{
    public partial class frmMain : Form
    {
        public delegate void Add_To_RichBoxDelegateType(string Add_it);
        Add_To_RichBoxDelegateType Add_To_RichBox; // delegate to add text into main richbox with log
        public delegate void Change_Menu_DelegateType(bool Change_To_What);
        Change_Menu_DelegateType Change_Menu_Item; // delegate to change visible items of menu
        public delegate void RefreshDataGridKeyWords_DelegateType();
        RefreshDataGridKeyWords_DelegateType Refresh_DataGridKeyWords; // delegate to refresh datagrid with keywords
        public delegate void RefreshDataGridFoundAuthors_DelegateType();
        RefreshDataGridFoundAuthors_DelegateType Refresh_DataGridFoundAuthors; // delegate to refresh datagrid with keywords
        public delegate void RefreshDataGridFoundUDK_DelegateType();
        RefreshDataGridFoundUDK_DelegateType Refresh_DataGridFoundUDK; // delegate to refresh datagrid with keywords

        public void Init_Delegates()
        {
            Add_To_RichBox = Add_RichBox; // delegate to add text into main richbox with log
            Change_Menu_Item = Change_M;  // delegate to change visible items of menu
            Refresh_DataGridKeyWords = Refresh_Griddi; // delegate to refresh datagrid with keywords
            Refresh_DataGridFoundAuthors = Refresh_Griddi_FoundAuthors; // delegate to refresh datagrid with found authors
            Refresh_DataGridFoundUDK = Refresh_DataGriddiFoundUDK;
        }

        private void Refresh_DataGriddiFoundUDK()
        {
            if (dataGridViewFoundUDK.InvokeRequired)
            {
                dataGridViewFoundUDK.Invoke(Refresh_DataGridFoundUDK);
            }
            else
            {
                var A = (from Files in DataTable_FILES.AsEnumerable()
                         join FoundAuthors in DataTable_FoundUDK.AsEnumerable()
                         on Files.Field<int>(Class_Table_FILES_Columns.ID_FullPathToFile) equals
                         FoundAuthors.Field<int>(Class_Table_FoundAuthors_Columns.ID_FullPathToFile)
                         into SomeData
                         from DataNya in SomeData
                         select new
                         {
                             FilePath = Files.Field<string>(Class_Table_FILES_Columns.FullPathToFile),
                             FoundUDK = DataNya.Field<string>(Class_Table_FoundAuthors_Columns.Author)
                         }).Distinct();

                dataGridViewFoundUDK.DataSource = A.ToList();
                dataGridViewFoundUDK.Refresh();
            }
        }

        private void Refresh_Griddi_FoundAuthors()
        {
            if (dataGridViewFoundAuthors.InvokeRequired)
            {
                dataGridViewFoundAuthors.Invoke(Refresh_DataGridFoundAuthors);
            }
            else
            {
                var A = (from Files in DataTable_FILES.AsEnumerable()
                         join FoundAuthors in DataTable_FoundAuthors.AsEnumerable()
                         on Files.Field<int>(Class_Table_FILES_Columns.ID_FullPathToFile) equals
                         FoundAuthors.Field<int>(Class_Table_FoundAuthors_Columns.ID_FullPathToFile)
                         into SomeData
                         from DataNya in SomeData
                         select new
                         {
                             FilePath = Files.Field<string>(Class_Table_FILES_Columns.FullPathToFile),
                             FoundAuthors = DataNya.Field<string>(Class_Table_FoundAuthors_Columns.Author)
                         }).Distinct();

                dataGridViewFoundAuthors.DataSource = A.ToList();
                dataGridViewFoundAuthors.Refresh();
            }
        }

        private void Refresh_Griddi()
        {
            if (dataGridViewFoundKeyWords.InvokeRequired)
            {
                dataGridViewFoundKeyWords.Invoke(Refresh_DataGridKeyWords);
            }
            else
            {
                int Rya;
                var A = (from Files in DataTable_FILES.AsEnumerable()
                         from Words in DataTable_WORDS.AsEnumerable()
                         join KeyWord in DataTable_KeyWords.AsEnumerable()
                         on new { A = Files.Field<int>(Class_Table_FILES_Columns.ID_FullPathToFile), B = Words.Field<int>(Class_Table_WORDS_Columns.ID_Word) } equals
                         new { A = KeyWord.Field<int>(Class_Table_KeyWords_Columns.ID_FullPathToFile), B = KeyWord.Field<int>(Class_Table_KeyWords_Columns.ID_Word) }
                         into SomeData
                         from DataNya in SomeData
                         select new
                         {
                             FilePath = Files.Field<string>(Class_Table_FILES_Columns.FullPathToFile),
                             KeyWords = int.TryParse(Words.Field<string>(Class_Table_WORDS_Columns.Word), out Rya) ?
                                 ((Char)Rya).ToString() : Words.Field<string>(Class_Table_WORDS_Columns.Word)
                         }).Distinct();

                dataGridViewFoundKeyWords.DataSource = A.ToList();
                dataGridViewFoundKeyWords.Refresh();
            }
        }

        private void Add_RichBox(string Add_it) // delegate to add text into main richbox with log
        {
            if (richTextBox_MainLog.InvokeRequired)
            {
                richTextBox_MainLog.Invoke(Add_To_RichBox, Add_it);
            }
            else
            {
                richTextBox_MainLog.AppendText(DateTime.Now.ToString() + " : " + Add_it + "\n");
            }
        }

        private void Change_M(bool Change_To_What) // delegate to change visible items of menu
        {
            if (menuStripMain.InvokeRequired)
            {
                menuStripMain.Invoke(Change_Menu_Item, Change_To_What);
            }
            else
            {
                создатьВнутреннееПредставлениеToolStripMenuItem.Enabled = Change_To_What;
                добавитьToolStripMenuItem.Enabled = Change_To_What;
                отметитьВыделенныеНаУдалениеToolStripMenuItem.Enabled = Change_To_What;
                остановитьСозданиеВнутренненогоПредставленияToolStripMenuItem.Enabled = !Change_To_What;
                искатьПоВсемСловамToolStripMenuItem.Enabled = Change_To_What;
                искатьПоКлючевымСловамToolStripMenuItem.Enabled = Change_To_What;
                искатьПоАвторуToolStripMenuItem.Enabled = Change_To_What;
            }
        }
    }
}
